#include <stdint.h>
#include <string.h>
#include "lib_jpeg_adapt.h"
#include "lib_png_adapt.h"
#include "util_bmp.h"
#include "image.h"

static char* GetTail(char* file)
{
    int32_t len = strlen(file);
    char* ret = NULL;
    do {
        ret = &file[--len];
        if (*ret == '.')
            return ret + 1;
    } while (len > 0);
    return ret;
}

Image* UtilPicGet(const char* file)
{
    Image* image = NULL;
    if (!file)
        return image;
    uint32_t width, height, pb;
    uint8_t* mem = NULL;
    char *tail = GetTail((char*)file);
    if (strlen(tail) >= 3)
    {
        if ((tail[0] == 'j' || tail[0] == 'J') && (tail[1] == 'p' || tail[1] == 'P'))
            mem = LibJpegGetBGRA(file, &width, &height, &pb);
        else if ((tail[0] == 'p' || tail[0] == 'P') && (tail[1] == 'n' || tail[1] == 'N'))
            mem = LibPngGet(file, &width, &height, &pb, LPT_BGRA);
        else if ((tail[0] == 'b' || tail[0] == 'B') && (tail[1] == 'm' || tail[1] == 'M'))
            mem = UtilBmpGetBGRA(file, &width, &height, &pb);
        if (mem)
            image = new Image(width, height, IMAGE_FORMAT_BGRA, mem);
    }
    return image;
}

Image* UtilPicGet(const char* file, ImageFormat format, int32_t width, int32_t height)
{
    Image* srcImage = UtilPicGet(file);
    if (!srcImage)
        return srcImage;

    Image* retImage = new Image();

    if ((width == 0 || width == srcImage->Width()) && 
        (height == 0 || height == srcImage->Height()))
    {
        if (format != srcImage->Format())
            srcImage->Translate(srcImage->Width(), srcImage->Height(), format, *retImage);
        else
        {
            delete retImage;
            return srcImage;
        }
    }
    else if (width > 0 && height > 0) {
        srcImage->Translate(width, height, format, *retImage);
    }
    else if (width > 0) {
        height = width * srcImage->Height() / srcImage->Width();
        srcImage->Translate(width, height, format, *retImage);
    }
    else {
        width = height * srcImage->Width() / srcImage->Height();
        srcImage->Translate(width, height, format, *retImage);
    }

    delete srcImage;
    return retImage;
}

void UtilPicCreate(const char* file, Image* image)
{
    if (!file || !image || !image->Mem().addr)
        return;
    
    char *tail = GetTail((char*)file);
    if (strlen(tail) >= 3)
    {
        if ((tail[0] == 'j' || tail[0] == 'J') && (tail[1] == 'p' || tail[1] == 'P'))
            LibJpegCreate(file, image->Mem().addr, image->Width(), image->Height(), image->Pb(), 75);
        else if ((tail[0] == 'b' || tail[0] == 'B') && (tail[1] == 'm' || tail[1] == 'M'))
            UtilBmpCreate(file, image->Mem().addr, image->Width(), image->Height(), image->Pb());
        else //if ((tail[0] == 'p' || tail[0] == 'P') && (tail[1] == 'n' || tail[1] == 'N'))
            LibPngCreate(file, image->Mem().addr, image->Width(), image->Height(), (LibPngType)image->Format());
    }
    else
        LibPngCreate(file, image->Mem().addr, image->Width(), image->Height(), (LibPngType)image->Format());
}
